#include <windows.h>
#include <GL/glut.h>
#include "ScanConvert.h"
#include "PolygonDrawer.h"
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

/******************************************************************
	Notes:
	Image size is 400 by 400 by default.  You may adjust this if
		you want to.
	You can assume the window will NOT be resized.
	Call clearFramebuffer to clear the entire framebuffer.
	Call setFramebuffer to set a pixel.  This should be the only
		routine you use to set the color (other than clearing the
		entire framebuffer).  drawit() will cause the current
		framebuffer to be displayed.
	As is, your scan conversion should probably be called from
		within the display function.  There is a very short sample
		of code there now.
	You may add code to any of the subroutines here,  You probably
		want to leave the drawit, clearFramebuffer, and
		setFramebuffer commands alone, though.
  *****************************************************************/

float framebuffer[ImageH][ImageW][3];
vector< vector<Pt> > polys;

/**
 *	Imports information about a polygon from a .obj file (in .obj format)
 *	  Assumes that the coordinates are in order (type, x, y, z) and that
 *	  nothing is specified to greater than 10 digits.
 */
void importPolygon(char* filename)
{
/*
	FILE* objFile;
	objFile = fopen(filename, "r");
*/
	printf("file is:\t%s\n",filename);

	vector<Pt> currPoly;
	char type[10], cx[10], cy[10], cz[10], space[256];
	float x,y,z;


	std::ifstream objFile(filename, ifstream::in);
	while(objFile.good())	// not EOF
	{
		objFile.getline(type, 10, ' ');	// use ' ' as delimiter
	
		if(type[0] == '#')
		{
			printf("Comment Line\n");
			objFile.getline(space,256);	// get the rest of the commented line
			continue;
		}
		if(type[0] == 'v')
		{
			objFile.getline(cx,10,' ');	// use ' ' as delimiter
			objFile.getline(cy,10,' ');	// use ' ' as delimiter
			objFile.getline(cz,10);		// will use '\n' as delimiter

			x = atof(cx);
			y = atof(cy);
			z = atof(cz);

			// For testing
			printf("%s | %f | %f | %f\n", type,x,y,z);

			// Handle adding vertex


		}
		if(type[0] == 'f')	// face
		{
			objFile.getline(cx,10,' ');	// use ' ' as delimiter
			objFile.getline(cy,10,' ');	// use ' ' as delimiter
			objFile.getline(cz,10);		// will use '\n' as delimiter


			x = atoi(cx);
			y = atoi(cy);
			z = atoi(cz);

			// For testing
			printf("%s | %f | %f | %f\n", type,x,y,z);

			// Handle face


		}



	}
}



// Draws the scene
void drawit(void)
{
   glDrawPixels(ImageW,ImageH,GL_RGB,GL_FLOAT,framebuffer);
   glFlush();
}

// Clears framebuffer to black
void clearFramebuffer()
{
	int i,j;

	for(i=0;i<ImageH;i++) {
		for (j=0;j<ImageW;j++) {
			framebuffer[i][j][0] = 0.0;
			framebuffer[i][j][1] = 0.0;
			framebuffer[i][j][2] = 0.0;
		}
	}
}

// Sets pixel x,y to the color RGB
// I've made a small change to this function to make the pixels match
// those returned by the glutMouseFunc exactly - Scott Schaefer 
void setFramebuffer(int x, int y, float R, float G, float B)
{
	// changes the origin from the lower-left corner to the upper-left corner
	y = ImageH - 1 - y;
	if (R<=1.0)
		if (R>=0.0)
			framebuffer[y][x][0]=R;
		else
			framebuffer[y][x][0]=0.0;
	else
		framebuffer[y][x][0]=1.0;
	if (G<=1.0)
		if (G>=0.0)
			framebuffer[y][x][1]=G;
		else
			framebuffer[y][x][1]=0.0;
	else
		framebuffer[y][x][1]=1.0;
	if (B<=1.0)
		if (B>=0.0)
			framebuffer[y][x][2]=B;
		else
			framebuffer[y][x][2]=0.0;
	else
		framebuffer[y][x][2]=1.0;
}

void display(void)
{
	clearFramebuffer ( );
	for ( int i = 0; i < polys.size ( ); i++ )
	{
		drawPolygon ( polys [ i ], Color ( ( ( i + 1 ) % 3 ) / 2.0, ( ( ( i + 1 ) / 3 ) % 3 ) / 2.0, ( ( ( i + 1 ) / 9 ) % 3 ) / 2.0 ) );
	}
	drawit();

	glFlush ( );
	glutSwapBuffers ( );
}

void init(void)
{
	clearFramebuffer();
	glOrtho ( -1, 1, -1, 1, -1, 1 );	// changed from depending on ImageH and ImageW




	// test drawing

/*
	vector<Pt> pts;

	pts.push_back ( Pt ( 10, 40 ) );
	pts.push_back ( Pt ( 10, 360 ) );
	pts.push_back ( Pt ( 390, 390 ) );
	pts.push_back ( Pt ( 320, 120 ) );

	polys.push_back ( pts );
*/


}

void idle ( void )
{
	glutPostRedisplay ( );
}

int main(int argc, char** argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutInitWindowSize(ImageW,ImageH);
	glutInitWindowPosition(10,10);
	glutCreateWindow("John Lake - Homework 4");
	glutIdleFunc ( idle );
	init();	

	char* objFile = "cube.obj";
	importPolygon(objFile);
//	importPolygon(argv[1]);	// omitted for testing purposes
	
	glutDisplayFunc(display);
	glutMainLoop();
	return 0;
}
